---
title: JavaScript Client
description: Documentation for the Trench JavaScript client
---

import TrenchScript from '/snippets/trench-script.mdx';

## Overview

`trench-js` is a client library for both web and Node.js environments that allows you to send events to any Trench instance. This library is designed to be easy to use and integrate into your existing projects. The library is built on top of [Analytics](https://github.com/DavidWells/analytics) and supports all of its [methods](https://github.com/DavidWells/analytics#usage). It can be installed via a package manager or via a CDN script tag.

## Installation via package manager

Install `trench-js` using your preferred package manager:

```bash
npm install trench-js
```

You can now initialize the client with your Trench instance URL and public API key:

```ts
import Trench from 'trench-js'

const trench = new Trench({
  serverUrl: 'your-server-url',
  publicApiKey: 'your-public-api-key',
})
```

For example, you can identify a user like this:

```ts
trench.identify('user-id', {
  email: 'test@example.com',
})
```

And track an event like this:

```ts
trench.track('test-event', {
  test: 'test-value',
})
```

Or to record a trigger page view:

```ts
trench.page()
```

## Installation via CDN script tag

Alternatively, you can use the CDN version:

<TrenchScript />

This will automatically add the `trench` object to the global scope and you can start tracking events automatically as `autoCaptureEvents` is set to `true` by default.
